<?php
//分页类,此处仅处理分页的各种计算,不处理分页的显示方式
/*
使用方法

$count = TbfDatabase::query('select count(*) from admin_user');
$count = TbfArray::first(TbfArray::first($count));
//分页
$pager = new TbfPage();
$pager->CurrentPage = TbfInput::inNum('page');
$pager->baseUrl ='/admin.php/Document/Search?CatId=20&search='.urlv($da);
$pager->ItemPerPage = 10;
$pager->ShowPageNum = 10;
$pager->TotalItem = $count;
$pager->Init();
$result = TbfDatabase::query('select * from admin_user limit ?,? ',
     array($pager->GetMysqlOffset(),$pager->ItemPerPage));

 */
use TbfDatabase\MysqlAST\SelectCommand;
class TbfPage {
    /**
     * @param SelectCommand $select
     * @param $baseUrl
     * @param int $itemPerPage
     * @return TbfPage
     * @throws Exception
     */
    static function createFromSelectCommand(SelectCommand $select,$baseUrl,$itemPerPage=20){
        //分页
        $pager = new TbfPage();
        $pager->baseUrl = $baseUrl;
        $pager->ItemPerPage = $itemPerPage;
        return $pager->runSelectCommand($select);
    }
    public $ItemPerPage = 10;       //每页数据量
    public $CurrentPage = null;     //当前页数,最少1页
    public $TotalItem;              //总数据量
    public $ShowPageNum = 10;       //显示分页的页面的时候,显示出来的页数 如 10,11,12,13,14,15 就是显示了6个页
    public $baseUrl = null;         //不包含页码参数的url,具体渲染url的时候会把页面参数加上
    public $pageKeyName = 'page';   //页面参数的名字
    public $data;                   //本次分页查询到的数据

    /**
     * 使用selectCommand获取分页数据和总数据量
     * 必须要有 baseUrl 参数,其他参数都有默认值
     * @param SelectCommand $select
     * @return TbfPage
     */
    function runSelectCommand(SelectCommand $select){
        if ($this->baseUrl===null){
            throw new Exception('runSelectCommand need baseUrl parameter');
        }
        $countSelect = $select->Copy();
        $countSelect->select('count(*) as c');
        $countData = TbfDatabase::runSelectCommand($countSelect);
        $this->TotalItem = $countData[0]['c'];
        $this->Init();

        $dataSelect = $select->Copy();
        $dataSelect->limit(intval($this->GetMysqlOffset() ).','.intval($this->ItemPerPage));
        $this->data = TbfDatabase::runSelectCommand($dataSelect);
        return $this;
    }

    function Init(){
        $this->CurrentPage = intval($this->CurrentPage);
        $this->ItemPerPage = intval($this->ItemPerPage);
        $this->TotalItem = intval($this->TotalItem);
        $this->ShowPageNum = intval($this->ShowPageNum);
        if ($this->CurrentPage===null){
            $this->CurrentPage = TbfInput::inNum('page');
        }
        if ($this->CurrentPage<1){
            $this->CurrentPage = 1;
        }
        if ($this->CurrentPage>$this->GetTotalPage()){
            $this->CurrentPage = $this->GetTotalPage();
        }
        if ($this->ShowPageNum <=0){
            $this->ShowPageNum = 10;
        }
    }

    //一共有多少页,最大页面(包含),最少1页
    function GetTotalPage(){
        $ret = (int)ceil(($this->TotalItem)/($this->ItemPerPage));
        if ($ret<=0){
            $ret = 1; //至少有1页
        }
        return $ret;
    }
    //一共有多少项
    function GetTotalItem(){
        return $this->TotalItem;
    }

    //是否有向前的按钮
    function IsBeforePageActive(){
        return $this->CurrentPage-1>=1;
    }
    //是否有向后的按钮
    function IsAfterPageActive(){
        return $this->CurrentPage+1<=$this->GetTotalPage();
    }
    //mysql数据库的偏移量
    function GetMysqlOffset(){
        $ret = ($this->CurrentPage-1)*$this->ItemPerPage;
        return $ret;
    }
    //前一页的url
    function GetBeforePageUrl(){
        $pageNum = $this->CurrentPage-1;
        if ($pageNum<1){
            $pageNum = 1;
        }
        return $this->GetUrlWithPage($pageNum);
    }
    //后台一页的url
    function GetAfterPageUrl(){
        $pageNum = $this->CurrentPage+1;
        if ($pageNum>$this->GetTotalPage()){
            $pageNum = $this->GetTotalPage();
        }
        return $this->GetUrlWithPage($pageNum);
    }

    //中间显示的分页的数据
    function GetShowPageArray(){
        //页面比显示数量还少,快速通道
        if ($this->GetTotalPage()<=$this->ShowPageNum){
            return $this->getShowPageArrayFromNum(1,$this->GetTotalPage());
        }
        $start = $this->CurrentPage - floor($this->ShowPageNum/2);
        $end = $this->CurrentPage + floor($this->ShowPageNum/2)-1;
        if ($start<1){
            $end += 1-$start;
            $start = 1;
        }
        if ($end>$this->GetTotalPage()){
            $start = $start - ($end - $this->GetTotalPage())+1;
            $end = $this->GetTotalPage();
        }
        return $this->getShowPageArrayFromNum($start,$end);
    }
    private function getShowPageArrayFromNum($start,$end){
        $output = array();
        for ($i=$start;$i<=$end;$i++){
            $url = $this->GetUrlWithPage($i);
            $output[] = array(
                'IsCurrent'=>$i==$this->CurrentPage,
                'PageNum'=>$i,
                'Url'=>$url,
            );
        }
        return $output;
    }
    function GetUrlWithPage($pageNum){
        return TbfUrl::addParamToUrl($this->baseUrl,array($this->pageKeyName=>$pageNum));
    }
}
